package com.zhiyou100.javaweb.myservlet.day005;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @packageName: ${NAME}
 * @className: ${PACKAGE_NAME}
 * @Description: TODO cookie 测试
 * @author: yang
 * @date: 2020/5/27
 */
@WebServlet(name = "Day005ServletCookieTest", urlPatterns = "/day005/cookieDemo2")
public class Day005ServletCookieTest extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        //  设定编码集
        response.setContentType("text/html;charset=UTF-8");
        // 设定类型
        // 3.获取Cookie
        Cookie[] cookies = request.getCookies();
        // 获取数据，遍历Cookies
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                String name = cookie.getName();
                String value = cookie.getValue();
                System.out.println(name + " : " + value);
            }

        }

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
